package org.business.accounting.pages;

import java.util.Collection;
import java.util.Iterator;
import java.util.Vector;

import org.apache.tapestry5.annotations.ApplicationState;
import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SetupRender;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.business.accounting.beans.CartBean;
import org.business.accounting.beans.ComputerBean;
import org.business.accounting.beans.ComputerOrderBean;
import org.business.accounting.beans.LogOutHelperBean;
import org.business.accounting.components.BuyOrOrder;
import org.business.accounting.interfaces.BasicOrder;
import org.business.accounting.interfaces.BasicProduct;
import org.business.accounting.interfaces.Cart;
import org.business.accounting.interfaces.Computer;
import org.business.accounting.interfaces.LogOutHelper;
import org.business.accounting.interfaces.User;
import org.business.accounting.service.ComputerDAO;
import org.business.accounting.service.OrderDAO;
import org.business.accounting.service.ProductSourceDAO;
import org.business.accounting.service.UserDAO;

@SuppressWarnings("unused")
public class Preview {
	
	private String previewType;
	
	@ApplicationState
	@Property
	private User user;
	
	@Property
	private boolean userExists;
	
	@ApplicationState
	@Property
	private Cart cart;
	
	@Property
	private boolean cartExists;
	
	@Property
	private BasicOrder basicOrder;
	
	@Property
	private boolean basicOrderExists;
	
	@Inject
	private UserDAO userDAO;
	
	@Inject
	private ComputerDAO computerDAO;
	
	@Inject
	private ProductSourceDAO productSourceDAO;
	
	@Persist
	@Property
	private LogOutHelper logOutHelper;
	
	//
	// Computer Preview
	//
	
	@Property
	private String add = "option";
	
	@Property
	private BasicProduct basicProduct;
	
	@Component(parameters = {
			"isAdmin = user.admin",
			"row = basicProduct",
			"source = products"})
	private BuyOrOrder buyOrOrder;
	
	//
	// Geters and Seters
	//
	
	public boolean isAdmin() {
		if (userExists) {
			return user.isAdmin();
		}
		return false;
	}
	
	public Collection<User> getUsers() {
		return userDAO.retrieve();
	}
	
	public Collection<BasicProduct> getProducts() {
		if (previewType.equalsIgnoreCase("computer")) {
			Iterator<Computer> iterator = getComputers().iterator();
			BasicProduct basicProduct;
			Collection<BasicProduct> collection = new Vector<BasicProduct>();
			while (iterator.hasNext()) {
				basicProduct = (Computer) iterator.next();
				collection.add(basicProduct);
			}
			return collection;
		}
		
		return new Vector<BasicProduct>();
	}
	
	public Collection<Computer> getComputers() {
		if (user.isAdmin()) {
			return productSourceDAO.getComputers().values();
		}
		return computerDAO.retrieve();
	}
	
	public String getPreviewType() {
		return previewType;
	}

	public void setPreviewType(String previewType) {
		this.previewType = previewType;
	}
	
	//
	// Event handlers
	//
	
	Object onActivate() {
		if (userExists) {
			return null;
		}
		return Index.class;
	}
	
	@SetupRender
	public void onSetupRender() {
		if (previewType.equalsIgnoreCase("computer")) {
			basicProduct = new ComputerBean();
		}
		
		if (!cartExists) {
			cart = new CartBean();
		}
		
		logOutHelper = new LogOutHelperBean();
		logOutHelper.setCart(cart);
		if (userExists) {
			logOutHelper.setUser(user);
		}
	}
	
	void onActivate(String previewType) {
		this.previewType = previewType;
	}
	
	String onPassivate() {
		return this.previewType;
	}
	
	@InjectPage
	private Purchase purchase;
	
	@Inject
	private OrderDAO orderDAO;
	
	Object onActionFromBuyOrOrder(String key) {
		if (previewType.equalsIgnoreCase("computer")) {
			basicOrder = new ComputerOrderBean();
			if (user.isAdmin()) {
				basicOrder.setItem(productSourceDAO.getComputers().get(key));
			} else {
				basicOrder.setItem(computerDAO.get(key));
			}
		} else {
			System.out.println("PREVIEW OTHER ITEMS");
		}
		
		orderDAO.save(basicOrder);
		
		purchase.setBasicOrder(basicOrder);		
		return purchase;
	}
	
}
